package com.frame.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 *
 * 功能:数据转化操作类<br>
 * 参数:<br>
 * 作者:王海锋<br>
 * 时间:2015-11-25下午05:03:13<br>
 */
public class DataConvert {

    public static String toString(Object obj){
        if(obj==null)
            return "";
        else if(obj instanceof java.util.Date || obj instanceof java.sql.Date)
            return DateConvert.toString((java.util.Date)obj);
        else if(obj instanceof char[])
            return new String((char[])obj);
        else if(obj instanceof byte[])
            return new String((byte[])obj);
        return obj.toString();
    }
    public static String toString(Object obj,String format){
        if(obj==null)
            return "";
        else if(obj instanceof java.util.Date || obj instanceof java.sql.Date){
            if(format==null || "".equals(format))
                return DateConvert.toString((java.util.Date)obj);
            else{
                SimpleDateFormat sp=new SimpleDateFormat(format);
                return sp.format(obj);
            }
        }
        else if(obj instanceof char[])
            return new String((char[])obj);
        else if(obj instanceof byte[])
            return new String((byte[])obj);
        else if(obj instanceof String || obj instanceof Integer || obj instanceof java.math.BigDecimal || obj instanceof Double || obj instanceof Long || obj instanceof Byte){
            if(obj!=null && format!=null && !"".equals(format)){
                if(format.indexOf("[")!=-1){
                    String ff=format.replace("[", "").replace("]", "");
                    String[] ss=ff.split(",",-1);
                    try{
                        if(ss[0].indexOf(":")!=-1){
                            for(String str:ss){
                                String[] ss2=str.split("\\:");
                                if(obj.toString().equalsIgnoreCase(ss2[0]))
                                    return ss2[1];
                            }
                        }else{
                            int n=Integer.parseInt(obj.toString());
                            return ss[n];
                        }
                    }catch(Exception e){return obj.toString();}
                }
                else{
                    double d=Double.parseDouble(obj.toString());
                    return new java.text.DecimalFormat(format).format(d);
                }
            }
        }
        return obj.toString();
    }
    static public Object convertValue(Class c,String s)
    {
        if(s == null)
        {
            if(c.isPrimitive())
            {
                if(c == char.class)
                    return new Character(' ');
                else if(c == boolean.class)
                    return Boolean.FALSE;
                else
                    s = "0";
            }
            else
            {
                return null;
            }
        }
        if(c == String.class)
            return s;

        if(c == Integer.class || c == int.class)
            return new Integer(s);
        else if(c == Long.class || c == long.class)
            return new Long(s);
        else if(c == Short.class || c == short.class)
            return new Short(s);
        else if(c == Byte.class || c == byte.class)
            return new Byte(s);
        else if(c == Float.class || c == float.class)
            return new Float(s);
        else if(c == Double.class || c == double.class)
            return new Double(s);
        else if(c == Character.class || c == char.class)
            return new Character(s.charAt(0));
        else if(c == Boolean.class || c == boolean.class)
            return convertBoolean(s);
        else if(c == Date.class)
            return DateConvert.toDate(s);
        else if(c == java.sql.Time.class)
            return new java.sql.Time(DateConvert.toDate(s).getTime());
        else if(c == java.sql.Timestamp.class)
            return new java.sql.Timestamp(DateConvert.toDate(s).getTime());
        else if(c == java.sql.Date.class)
            return new java.sql.Date(DateConvert.toDate(s).getTime());
        else if(c == char[].class)
            return s.toCharArray();
        else
            return s.toString();
    }
    static public Boolean convertBoolean(String s)
    {
        if(s == null)
            return null;

        if("true".equals(s.toLowerCase()))
            return Boolean.TRUE;
        else
            return Boolean.FALSE;
    }

    /**
     *
     * 功能:double相减<br>
     * 参数:<br>
     * 作者:王海锋<br>
     * 时间:2015-11-25下午04:46:13<br>
     */
    public static double doubleSub(double... d){
        BigDecimal b=new BigDecimal(Double.toString(d[0]));
        for(int i=1;i<d.length;i++)
            b=b.subtract(new BigDecimal(Double.toString(d[i])));
        return b.doubleValue();
    }
    /**
     *
     * 功能:double相加<br>
     * 参数:<br>
     * 作者:王海锋<br>
     * 时间:2015-11-25下午04:46:37<br>
     */
    public static double doubleSum(double... d){
        BigDecimal b=new BigDecimal("0");
        for(int i=0;i<d.length;i++)
            b=b.add(new BigDecimal(Double.toString(d[i])));
        return b.doubleValue();
    }
    /**
     *
     * 功能:double相除<br>
     * 参数:d1/d2<br>
     * 作者:王海锋<br>
     * 时间:2015-11-25下午04:46:37<br>
     */
    public static double doubleDiv(double d1,double d2){
        if(d2==0)return 0;
        return new BigDecimal(d1).divide(new BigDecimal(d2), 10, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    /**
     *
     * 功能:数字相除<br>
     * 参数:d1/d2<br>
     * 作者:王海锋<br>
     * 时间:2015-11-25下午04:46:37<br>
     */
    public static double doubleDiv(int d1,int d2){
        if(d2==0)return 0;
        return new BigDecimal(d1).divide(new BigDecimal(d2), 10, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    /**
     *
     * 功能:数字相除<br>
     * 参数:d1/d2<br>
     * 作者:王海锋<br>
     * 时间:2015-11-25下午04:46:37<br>
     */
    public static double doubleDiv(double d1,int d2){
        if(d2==0)return 0;
        return new BigDecimal(d1).divide(new BigDecimal(d2), 10, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    /**
     *
     * 功能:数字相除<br>
     * 参数:d1/d2<br>
     * 作者:王海锋<br>
     * 时间:2015-11-25下午04:46:37<br>
     */
    public static double doubleDiv(int d1,double d2){
        if(d2==0)return 0;
        return new BigDecimal(d1).divide(new BigDecimal(d2), 10, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    /**
     *
     * 功能:double相乘<br>
     * 参数:<br>
     * 作者:王海锋<br>
     * 时间:2015-12-2下午02:53:03<br>
     */
    public static double doubleMul(double... d){
        BigDecimal b=new BigDecimal("1");
        for(int i=0;i<d.length;i++)
            b=b.multiply(new BigDecimal(Double.toString(d[i])));
        return b.doubleValue();
    }
    /**
     *
     * 功能:相乘<br>
     * 参数:<br>
     * 作者:王海锋<br>
     * 时间:2015-12-2下午02:53:03<br>
     */
    public static double doubleMul(int n,double d){
        BigDecimal b=new BigDecimal(n);
        return b.multiply(new BigDecimal(Double.toString(d))).doubleValue();
    }
    /**
     *
     * 功能:相乘<br>
     * 参数:<br>
     * 作者:王海锋<br>
     * 时间:2015-12-2下午02:53:03<br>
     */
    public static double doubleMul(double d,int n){
        BigDecimal b=new BigDecimal(d);
        return b.multiply(new BigDecimal(n)).doubleValue();
    }
    /**
     *
     * 功能:保留小数点<br>
     * 参数:rounding是否四舍弃五入<br>
     * 作者:王海锋<br>
     * 时间:2015-12-2下午02:55:46<br>
     */
    public static double toFixed(double d,int scale,boolean rounding){
        if(rounding){
            return new java.math.BigDecimal(d).setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();
        }else{
            double m=new java.math.BigDecimal(d).setScale(scale+4,BigDecimal.ROUND_HALF_UP).doubleValue();
            String s=doubleFormat(m,scale+4).replaceAll("([0-9]+\\.[0-9]{"+scale+"})[0-9]*","$1");
            return Double.parseDouble(s);
        }
    }

    public static String format(double d,String format){
        return new DecimalFormat(format).format(d);
    }
    public static String removeZero(String val){
        if(val==null)return "";
        String s=val;
        while(s.endsWith("0")){
            int n=s.lastIndexOf("0");
            s=s.substring(0,n);
        }
        if(s.endsWith(".")){
            int n=s.lastIndexOf(".");
            s=s.substring(0,n);
        }
        return s;
    }
    public static String removeZero(Double val){
        if(val==null)return "";
        String s=doubleFormat(val,4);
        while(s.endsWith("0")){
            int n=s.lastIndexOf("0");
            s=s.substring(0,n);
        }
        if(s.endsWith(".")){
            int n=s.lastIndexOf(".");
            s=s.substring(0,n);
        }
        return s;
    }
    public static String doubleFormat(Double data){
        return new BigDecimal(data).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }
    public static String doubleFormat(Double data,int scale){
        return new BigDecimal(data).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }
}
